package com.xyzq.hs;

import java.util.ArrayList;
import java.util.List;

import com.xyzq.hs.util.Logger;
import com.xyzq.hs.util.LoggerFactory;
import com.xyzq.hs.util.StringUtils;

public class Response {
	
	Logger log=LoggerFactory.getLogger(Response.class);
	private short resultCount;
	private short responseCode;
	private String errorMessage;
	private int dataSize;
	private List<ResultSet> resultSets=new ArrayList<ResultSet>();
	
	public Response(byte[] body){
		ByteReader reader=new ByteReader(body);
		short resultCount = reader.readShort();
		log.debug("Response result count:{}",resultCount);
		for(int rc=0;rc<resultCount;rc++){
			this.responseCode =reader.readShort();
			this.dataSize =reader.readInt(4);
			log.debug("datasize:{}",this.dataSize);
			if(this.responseCode!=0){
				this.errorMessage=reader.readStr(dataSize);
				ResultSet resultSet=new ResultSet(this.responseCode,this.errorMessage);
				resultSets.add(resultSet);
			}else{
				//read resultSetMetaData
				log.debug("================================");
				log.debug("Parse ResultSetMetaData");
				short  columnCount=reader.readShort();
				short  rowCount=reader.readShort();
				short[] columnLens=new short[columnCount];
				String[] columnNames=new String[columnCount];
				char[] columnTypes=new char[columnCount];
				for(int i=0;i<columnCount;i++){
					columnLens[i]=reader.readShort();
					columnNames[i]=StringUtils.fieldNameToPropertyName(reader.readStr(columnLens[i]));
					columnTypes[i]=reader.readChar();
					log.debug("Column len:{},name:{},type:{}",columnLens[i],columnNames[i],columnTypes[i]);
				}			
				
				ResultSetMetaData meta = new ResultSetMetaData(columnCount,columnNames,columnTypes);
				//read readDataSet
				log.debug("================================");
				log.debug("Read DataSet");
				byte[][][] dataSet = readDataSet(reader, columnCount, rowCount, columnTypes);
				ResultSet resultSet=new ResultSet(meta, dataSet, rowCount);
				resultSet.setResponseCode(this.responseCode);
				resultSet.setErrorMessage(this.errorMessage);
				resultSets.add(resultSet);
			}
		}
		
	}

	private byte[][][] readDataSet(ByteReader reader, short columnCount, short rowCount,char[] columnTypes) {
		byte[][][] dataSet=new byte[rowCount][columnCount][];
		for(int row=0;row<rowCount;row++){
			for(int col=0;col<columnCount;col++){
				char columnType = columnTypes[col];
				switch(columnType){
					case 'I':
						dataSet[row][col]=reader.read(4);
						break;
					case 'F':
						dataSet[row][col]=reader.read(8);
						break;
					case 'C':
						dataSet[row][col]=reader.read(1);
						break;
					case 'S':
						short len=reader.readShort();
						dataSet[row][col]=reader.read(len);
						break;
				}
					
			}
			
		}
		return dataSet;
	}

	public short getResultCount() {
		return resultCount;
	}

	public short getResponseCode() {
		return responseCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public int getDataSize() {
		return dataSize;
	}

	public List<ResultSet> getResultSets() {
		return resultSets;
	}
}
